/*----------------------------------------------------------------------*
 *	CLASS:		Qt3Util
 *	Quantum Tic Tac Toe
 *	Copyright (c) 2009 Novatia Labs, Inc.  All rights reserved.
 *----------------------------------------------------------------------*/
package com.novatialabs.qttt.util;

import com.novatialabs.qttt.engine.Qt3Player;
import com.novatialabs.qttt.engine.Qt3Square;
import com.novatialabs.qttt.engine.impl.Qt3SquareImpl;

/**
 * Utility functions for QT3 code.
 * 
 * @author duane
 */
public class Qt3Util
{

	private Qt3Util() {} // hide constructor -- interface is static

	public static void appendQuantumBoard(
			final StringBuilder	buf,
			final Qt3Square[]	board)
	{
		final boolean debugTangles = true;
		final String threeBlanks = "   ";

		for ( int startSqIndex = 0 ; startSqIndex <= 6 ; startSqIndex += 3 ) {
			final Qt3Square sqA = board[startSqIndex];
			final Qt3Square sqB = board[startSqIndex+1];
			final Qt3Square sqC = board[startSqIndex+2];
			final Qt3Player ownsA = sqA.getOwner();
			final Qt3Player ownsB = sqB.getOwner();
			final Qt3Player ownsC = sqC.getOwner();

			if ( startSqIndex != 0 ) {
				buf.append("===#===#===\n");
			}

			if ( ownsA != null ) {
				buf.append(threeBlanks);
			} else {
				buf.append((sqA.isMoveInSquare(1)?'X':' '));
				buf.append((sqA.isMoveInSquare(2)?'O':' '));
				buf.append((sqA.isMoveInSquare(3)?'X':' '));
			}
			buf.append('#');
			if ( ownsB != null ) {
				buf.append(threeBlanks);
			} else {
				buf.append((sqB.isMoveInSquare(1)?'X':' '));
				buf.append((sqB.isMoveInSquare(2)?'O':' '));
				buf.append((sqB.isMoveInSquare(3)?'X':' '));
			}
			buf.append('#');
			if ( ownsC != null ) {
				buf.append(threeBlanks);
			} else {
				buf.append((sqC.isMoveInSquare(1)?'X':' '));
				buf.append((sqC.isMoveInSquare(2)?'O':' '));
				buf.append((sqC.isMoveInSquare(3)?'X':' '));
			}
			buf.append('\n');

			if ( ownsA != null ) {
				buf.append(' ');
				buf.append(ownsA);
				buf.append(sqA.getOwningMove());
			} else {
				buf.append((sqA.isMoveInSquare(4)?'O':' '));
				buf.append((sqA.isMoveInSquare(5)?'X':' '));
				buf.append((sqA.isMoveInSquare(6)?'O':' '));
			}
			buf.append('#');
			if ( ownsB != null ) {
				buf.append(' ');
				buf.append(ownsB);
				buf.append(sqB.getOwningMove());
			} else {
				buf.append((sqB.isMoveInSquare(4)?'O':' '));
				buf.append((sqB.isMoveInSquare(5)?'X':' '));
				buf.append((sqB.isMoveInSquare(6)?'O':' '));
			}
			buf.append('#');
			if ( ownsC != null ) {
				buf.append(' ');
				buf.append(ownsC);
				buf.append(sqC.getOwningMove());
			} else {
				buf.append((sqC.isMoveInSquare(4)?'O':' '));
				buf.append((sqC.isMoveInSquare(5)?'X':' '));
				buf.append((sqC.isMoveInSquare(6)?'O':' '));
			}

			if ( debugTangles ) {
				buf.append("\t\t ");
				buf.append(((Qt3SquareImpl)sqA).getTangleIndex());
				buf.append(" | ");
				buf.append(((Qt3SquareImpl)sqB).getTangleIndex());
				buf.append(" | ");
				buf.append(((Qt3SquareImpl)sqC).getTangleIndex());
			}
			buf.append('\n');

			if ( ownsA != null ) {
				buf.append(threeBlanks);
			} else {
				buf.append((sqA.isMoveInSquare(7)?'X':' '));
				buf.append((sqA.isMoveInSquare(8)?'O':' '));
				buf.append((sqA.isMoveInSquare(9)?'X':' '));
			}
			buf.append('#');
			if ( ownsB != null ) {
				buf.append(threeBlanks);
			} else {
				buf.append((sqB.isMoveInSquare(7)?'X':' '));
				buf.append((sqB.isMoveInSquare(8)?'O':' '));
				buf.append((sqB.isMoveInSquare(9)?'X':' '));
			}
			buf.append('#');
			if ( ownsC != null ) {
				buf.append(threeBlanks);
			} else {
				buf.append((sqC.isMoveInSquare(7)?'X':' '));
				buf.append((sqC.isMoveInSquare(8)?'O':' '));
				buf.append((sqC.isMoveInSquare(9)?'X':' '));
			}
			buf.append('\n');

		}
	} // printQuantumBoard()

}
